<?php

namespace app\controllers;
use yii\db\Query;
use yii\web\Controller;


class ApiIndexController extends ApiBaseController {

    //首页
   public function actionIndex(){
   	 $data=\Yii::$app->request->get();
   	 $where['status']=10; 
     $where['city']=$this->GetIdByRegion($data['city']);  
     $where['hot']=10; 
     $banner=$this->GetBanner();
     $company=$this->GetHotCompany();
     $work=$this->Work($where);
     $article=$this->GetArticle();
     return json_encode(['banner'=>$banner,'company'=>$company,'work'=>$work,'article'=>$article]);
   }

      //通过城市名查ID
     public function GetIdByRegion($name){
     	$list=(new Query())->select('id')->from('yii_region')->where(['like','name',$name])->andWhere(['level'=>2])->one();
     	return $list['id'];
     	
     }

    //求职助手
    public function GetArticle(){
        $list=(new Query())->select('id,cover,title,desc')->from('yii_article')->where(['status'=>10])->limit(3)->orderBy('create_time desc')->all();
        foreach ($list as $k=>$v){
            $list[$k]['cover']=$this->GetImg($v['cover']);
        }
        return $list;
    }

    //职位
    public function Work($where){
    	 if(is_null($where['city'])){
    	 	 $list=(new Query())->select('id,name,city,company,region,experience,education,min_salary,max_salary,welfare')->from('yii_work')->where(['status'=>10,'hot'=>10])->limit(6)->all();
    	 }else{
    	   $list=(new Query())->select('id,name,city,company,region,experience,education,min_salary,max_salary,welfare')->from('yii_work')->where($where)->limit(6)->all();	
    	 }
     
         foreach ($list as $k=>$v){
          $list[$k]['city']=$this->GetRegion($v['city']);
          $list[$k]['region']=$this->GetRegion($v['region']);
          $list[$k]['experience']=$this->GetExperience($v['experience']);
          $list[$k]['education']=$this->GetEducation($v['education']);
          $list[$k]['welfare']=$this->GetWelfare($v['welfare']);
          $list[$k]['company']=$this->GetCompany($v['company']);
         }
        return $list;
    }

    //热门企业
    public function GetHotCompany(){
        $list=(new Query())->select('id,name,shorter_form')->from('yii_company')->where(['hot'=>10])->limit(6)->all();
        foreach($list as $k=>$v){
        $list[$k]['name']=$v['shorter_form'];	
        }
        return $list;
    }

   //轮播图
   public function GetBanner(){
       $list=(new Query())->select('img_id')->from('yii_banner')->where(['status'=>10])->orderBy('sort')->all();
       foreach ($list as $k=>$v){
        $list[$k]['img']=$this->GetImg($v['img_id']);
        unset($list[$k]['img_id']);
       }
       return $list;
   }

   //意见反馈
   public function actionOpinion(){
       $ache=\Yii::$app->cache;
       $data=\Yii::$app->request->post();
       $data['create_time']=time();
       $data['ip']=\Yii::$app->request->userIP;

       if (!$ache->get($data['ip'])){
           $ache->set($data['ip'],true,60);
           $res=\Yii::$app->db->createCommand()->insert('yii_user_opinion',$data)->execute();
           if ($res){
               return json_encode(['code'=>1,'msg'=>'提交成功']);
           }else{
               return json_encode(['code'=>0,'msg'=>'提交失败']);
           }
       }else{
            return json_encode(['code'=>0,'msg'=>'请勿频繁提交']);
       }


   }

}
